package com.ysd.crm.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.crm.entity.Students;
import com.ysd.crm.entity.Userchecks;
import com.ysd.crm.entity.seach.UserchecksSearch;
import com.ysd.crm.service.UserchecksService;
import com.ysd.crm.utils.LayResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RequestMapping("/userchecks")
@Api
@RestController
public class UserchecksController {
	
	@Autowired
	private UserchecksService uService;
	
	@ApiOperation("添加学生信息 Userchecks ydd")
	@GetMapping("getUserchecks")
	public Object getUserchecks(Userchecks u) {
		u.setPage((u.getPage()-1)*u.getLimit());
		List<Userchecks> list = uService.findUserchecks(u);
		int count = uService.findCount(u);
		if (list!=null) {
			return LayResult.toClient("0", "查询成功", count, list);
		} else {
			return LayResult.toClient("1", "查询失败");
		}
	}
	
	
	@ApiOperation("修改签到 ydd")
	@PostMapping("updateUserchecks")	
	public Object updateUserchecks(Userchecks u) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			u.setCheckOutTime(df.parse(df.format(new Date())));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int n= uService.updateUserchecks(u);
		if(n>0) {
			return LayResult.toClient("0", "修改成功");
		}
		return LayResult.toClient("1", "修改失败");
	}
	
	@ApiOperation("签到-gmw")
	@PostMapping("addUserchecks")
	public Object addUchecks(UserchecksSearch u) throws ParseException {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		u.setDate(sdf.format(new Date()));
//		u.setCheckInTime(sdf.parse(sdf.format(new Data())));
		Userchecks user=uService.findAll(u);
		//int count=uService.findCount(u);
		System.out.println(user);
		System.out.println(user==null);
		if(user==null) {
			u.setCheckInTime(new Date());
			int n=uService.insertUserchecks(u);
			if(n>0) {
				return LayResult.toClient("0", "签到成功");
			}
			return LayResult.toClient("1", "签到失败");
		}
		if(user!=null) {
			if(user.getCheckOutTime()==null) {
				user.setCheckOutTime(new Date());
				int n=uService.updateUchecks(user);
				if(n>0) {
					return LayResult.toClient("0", "已签退");
				}
				return LayResult.toClient("1", "签退失败");
			}
		}
		return LayResult.toClient("3", "不能重复打卡");
	}
	@ApiOperation("打卡状态-gmw")
	@PostMapping("getUsers")
	public Object getUsers(UserchecksSearch u) throws ParseException {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		u.setDate(sdf.format(new Date()));
//		u.setCheckInTime(sdf.parse(sdf.format(new Data())));
		Userchecks user=uService.findAll(u);
		//int count=uService.findCount(u);
		System.out.println(user);
		System.out.println(user==null);
		if(user==null) {			
			return LayResult.toClient("0", "签到");
		}
		if(user!=null) {
			if(user.getCheckOutTime()==null) {
				return LayResult.toClient("1", "签退");
			}
		}
		return LayResult.toClient("2", "已签退");
	}	
}
